<template>
		<div class="bgimg">
      <el-tabs v-model="activeName" @tab-click="handleClick">
					<el-tab-pane label="登录" name="second">
						<el-form :model="LForm" :rules="rules" ref="lForm" label-width="100px" class="demo-ruleForm">
							<el-form-item label="用户名" prop="username">
								<el-input v-model="LForm.username"></el-input>
							</el-form-item>
							<el-form-item label="密码" prop="password">
								<el-input v-model="LForm.password" show-password></el-input>
							</el-form-item>
						</el-form>
						<el-button @click="dialogVisible = false">取 消</el-button>
						<el-button type="primary" @click="openlogin('lForm')">登 录</el-button>
					</el-tab-pane>
					<el-tab-pane label="注册" name="first">
						<el-form :model="ZForm" :rules="zcrules" ref="zForm" label-width="100px" class="demo-ruleForm">
							<el-form-item label="用户名" prop="username">
								<el-input v-model="ZForm.username"></el-input>
							</el-form-item>
							<el-form-item label="密码" prop="password">
								<el-input v-model="ZForm.password" show-password></el-input>
							</el-form-item>
							<el-form-item label="真实姓名" prop="namezz">
								<el-input v-model="ZForm.namezz"></el-input>
							</el-form-item>
						</el-form>
						<el-button @click="dialogVisible = false">取 消</el-button>
						<el-button type="primary" @click="openreszc('zForm')">注 册</el-button>
					</el-tab-pane>
      </el-tabs>

		</div>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				loginuser: '',
				showlogin: true,
				showname: false,
				activeName: 'second',
				token: sessionStorage.getItem('token'),
				dialogVisible: false,
				LForm: {
					username: "",
					password: ""
				},
				ZForm: {
					namezz: "",
					username: "",
					password: ""
				},
				rules: {
					username: [{
						required: true,
						message: "请输入账号",
						trigger: "blur"
					}],
					password: [{
						required: true,
						message: "请输入密码",
						trigger: "blur"
					}]
				},
				zcrules: {
					username: [{
						required: true,
						message: "请输入账号",
						trigger: "blur"
					}],
					password: [{
						required: true,
						message: "请输入密码",
						trigger: "blur"
					}],
					namezz: [{
						required: true,
						message: "请输入姓名",
						trigger: "blur"
					}]
				}
			};
		},
		created() { //钩子函数页面加载完调用


		},
		methods: {

			// logout() {
			// 	this.$store.commit('del_token');
			// 	this.getToken();
			// },
			openlogin(lForm) {
				this.$refs[lForm].validate((valid) => {
					if (valid) {
						this.$httpApi.post('http://localhost:8088/login', this.LForm)
							.then(res => {
								if (res.data == '没有找到该用户') {
									this.$message.error('用户名不存在');
								} else if (res.data.msg == '密码错误') {
									this.$message.error('密码错误');
								} else {
									this.$refs.lForm.resetFields(); //清空表格数据
									this.dialogVisible = false;
									// 将用户token保存到vuex中
									this.$store.commit('set_token', res.data.token);
									this.$store.commit('set_username', res.data.username);
									this.loginuser = sessionStorage.getItem('username');
									this.token = sessionStorage.getItem('token');
									this.$message.success('登陆成功');
									this.$router.push({
										name: 'Index'
									})
								}
							});
					} else {
						this.$message.error('请填写之后再登录');
						return false;
					}
				});

			},
			openreszc(zForm) {
				this.$refs[zForm].validate((valid) => {
					if (valid) {
						this.$httpApi.post('http://localhost:8088/register', this.ZForm)
							.then(res => {
								if (res.data == '用户名已存在') {
									this.$message.error('用户名已存在');
								} else if (res.data == '注册成功') {
									this.$message.success('注册成功');
									this.$refs.zForm.resetFields(); //清空表格数据
									this.activeName = 'second';
								} else {
									this.$message.error('系统异常，请联系管理员');
								}
							});
					} else {
						this.$message.error('请填写之后再注册');
						return false;
					}
				});
			},
			handleClick(tab, event) {
				console.log(tab, event);
			},
			handleClose() {
				this.dialogVisible = false;
			}
		}
	};
</script>

<style scoped>
</style>
